package cmbchina.util;



import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhangmin on 16-4-8.
 * 解析json
 */
public class JacksonUitl {

    private static ObjectMapper objectMapper=null;
    private JacksonUitl(){

    }

    public static ObjectMapper getObjectMapper(){

        if(objectMapper==null){
            objectMapper = new ObjectMapper();
        }
        return objectMapper;
    }


    /**
     * json数据转为string
     * @param obj
     * @return
     */
    public static String toJson(Object obj){

        String result = "";
        try {
            result = getObjectMapper().writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }



    /***
     * json数据转为map对象
     * @param json
     * @return
     */
    public static Map<String,Object> jsonToMap(String json) {

        objectMapper  = getObjectMapper();
        Map<String,Object> result = null;

        try {
            result = objectMapper.readValue(json,Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

    /**
     * map 转换为 json
     */
    public void test01(){

        Map<String,String> map = new LinkedHashMap<String, String>();
        map.put("name","zhangSan");
        map.put("sex","1");
        try{

            String jsonStr = JacksonUitl.getObjectMapper().writeValueAsString(map);
            //解析json字符串
            JsonNode node = JacksonUitl.getObjectMapper().readTree(jsonStr);
            System.out.println(jsonStr);
            System.out.println("name="+node.get("name").asText());
            System.out.println("name="+node.get("name"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 解析json格式字符串
     */
    public void test02(){

        try{

            String jsonStr = "{\"data\":{\"birth_day\":7,\"birth_month\":6},\"errcode\":0,\"msg\":\"ok\",\"ret\":0}";
            //解析json字符串
            JsonNode node = JacksonUitl.getObjectMapper().readTree(jsonStr);
            JsonNode data = node.path("data");

            JsonNode birth_day = data.path("birth_day");
            System.out.println(birth_day.asInt());

            JsonNode birth_month = data.path("birth_month");
            System.out.println(birth_month.asInt());

            JsonNode msg = node.path("msg");
//            System.out.println(msg.getTextValue());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * json 直接提取 值
     */
    public static void MyTest05()
    {
        try
        {
            // 演示字符串
            String str = "{\"data\":{\"hasnext\":0,\"info\":[{\"id\":\"288206077664983\",\"timestamp\":1371052476},{\"id\":\"186983078111768\",\"timestamp\":1370944068},{\"id\":\"297031120529307\",\"timestamp\":1370751789},{\"id\":\"273831022294863\",\"timestamp\":1369994812}],\"timestamp\":1374562897,\"totalnum\":422},\"errcode\":0,\"msg\":\"ok\",\"ret\":0,\"seqid\":5903702688915195270}";

            ObjectMapper mapper = new ObjectMapper();
            JsonNode root = mapper.readTree(str);

            // 提取 data
            JsonNode data = root.path("data");
            // 提取 info
            JsonNode info = data.path("info");

            System.out.println(info.size());

            // 得到 info 的第 0 个
            JsonNode item = info.get(0);
            System.out.println(item.get("id"));
            System.out.println(item.get("timestamp"));

            // 得到 info 的第 2 个
            item = info.get(2);
            System.out.println(item.get("id"));
            System.out.println(item.get("timestamp"));

            // 遍历 info 内的 array
            if (info.isArray())
            {
                for (JsonNode objNode : info)
                {
                    System.out.println(objNode);
                }
            }

        }
        catch (Exception e)
        {

        }
    }

    /**
     * 创建一个 json，并向该 json 添加内容
     */
    public static void MyTest07()
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode root1 = mapper.createObjectNode();

            root1.put("nodekey1", 1);
            root1.put("nodekey2", 2);

            System.out.println(root1.toString());

            //Create the root node
            ObjectNode root = mapper.createObjectNode ();
            //Create a child node
            ObjectNode node1 = mapper.createObjectNode ();
            node1.put ("nodekey1", 1);
            node1.put ("nodekey2", 2);
            //Bind the child nodes
            root.put ("child", node1);
            //Array of nodes
            ArrayNode arrayNode = mapper.createArrayNode ();
            arrayNode.add (node1);
            arrayNode.add (1);
            //Bind array node
            root.put ("arraynode", arrayNode);

            System.out.println (mapper.writeValueAsString (root));
            // 得到的输出信息
            // {"child":{"nodekey1":1,"nodekey2":2},"arraynode":[{"nodekey1":1,"nodekey2":2},1]}
        }
        catch (Exception e)
        {

        }
    }

    // 创建一个 array node
    public static void MyTest08()
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            ArrayNode arrayNode = mapper.createArrayNode();

            int i = 0;
            // 在 array 内创建 3 组 node 存入 array
            for (i = 0; i < 3; i++)
            {
                // 创建一个 node
                ObjectNode node = mapper.createObjectNode();

                node.put("nodeA", i);
                node.put("nodeB", i);
                node.put("nodeC", i);

                // 向 array 内添 node
                arrayNode.add(node);
            }

            // 根
            ObjectNode root = mapper.createObjectNode();
            root.put("total", i);
            root.put("rows", arrayNode);

            System.out.println(mapper.writeValueAsString(root));
            // 得到的输出信息
            // {"total":3,"rows":[{"nodeA":0,"nodeB":0,"nodeC":0},{"nodeA":1,"nodeB":1,"nodeC":1},{"nodeA":2,"nodeB":2,"nodeC":2}]}
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String [] args){

        new JacksonUitl().test01();
        new JacksonUitl().test02();
        JacksonUitl.MyTest05();
        JacksonUitl.MyTest07();
        JacksonUitl.MyTest08();
    }
}
